package com.whmnx.main.ui.adapter;

import android.content.Context;
import android.os.Environment;
import android.widget.Button;

import com.classic.adapter.BaseAdapterHelper;
import com.classic.adapter.CommonRecyclerAdapter;
import com.tencent.qcloud.ugckit.utils.ToastUtil;
import com.whmnx.base.bean.MusicBean;
import com.whmnx.base.constants.Constants;
import com.whmnx.base.utils.ImageUtil;
import com.whmnx.main.R;
import com.whmnx.main.ui.videorecord.TCVideoRecordActivity;

import io.reactivex.annotations.NonNull;
import me.goldze.mvvmhabit.http.DownLoadManager;
import me.goldze.mvvmhabit.http.download.ProgressCallBack;
import okhttp3.ResponseBody;

/**
 * author : huang
 * date   : 2020/10/19 0019 9:17
 * version: 1.0
 * @author Administrator
 */
public class MusicListAdapter extends CommonRecyclerAdapter<MusicBean> {
    private TCVideoRecordActivity context;
    private OnClickSubItemListener mOnClickSubItemListener;
    private int pos;

    public void setOnClickSubItemListener(OnClickSubItemListener onClickSubItemListener) {
        mOnClickSubItemListener = onClickSubItemListener;
    }

    public interface OnClickSubItemListener {
        void onClickUseBtn(Button button, int position,String s);
    }


    public MusicListAdapter(Context context) {
        super(context, R.layout.item_music);
        this.context = (TCVideoRecordActivity)context;
    }

    public void updateItem(int position, @NonNull MusicBean info) {
        pos = position;
          notifyDataSetChanged();
    }


    @Override
    public void onUpdate(BaseAdapterHelper helper, MusicBean item, int position) {
        helper.setText(R.id.tv_Product_Name, (String) item.getMusic_Title());
        helper.setText(R.id.tv_Product_SPEC, (String) item.getMusic_Author());

        ImageUtil.setImage(helper.getView(R.id.iv_img),item.getMusic_Img());

        Button btnUse =  helper.getView(R.id.tv_update);
        btnUse.setOnClickListener(v -> {
            String loadUrl = Constants.HOST + item.getMusic_Url();

         //   File onlineMaterialDir = VideoDeviceUtil.getExternalFilesDir(UGCKit.getAppContext(), "bgm");
          //  File onlineMaterialDir = Environment.getExternalStorageDirectory().getPath();
            String destFileDir = Environment.getExternalStorageDirectory().getPath() + "bgm/";  //文件存放的路径
            String destFileName = System.currentTimeMillis() + ".mp3";//文件存放的名称


           /* if (pos == position){
                helper.setText(R.id.tv_update, "使用");
                helper.setTextColor(R.id.tv_update, context.getResources().getColor(R.color.white));
                helper.setBackgroundRes(R.id.tv_update, R.drawable.red_drawable_oval);
            }*/


            if (btnUse.getText().toString().equals("下载")) {
              //  TCMusicManager.getInstance().downloadMusicInfo(System.currentTimeMillis() + "", position, loadUrl);

                DownLoadManager.getInstance().load(loadUrl, new ProgressCallBack<ResponseBody>(destFileDir, destFileName) {
                    @Override
                    public void onStart() {
                        //RxJava的onStart()
                    }

                    @Override
                    public void onCompleted() {
                        //RxJava的onCompleted()
                    }

                    @Override
                    public void onSuccess(ResponseBody responseBody) {
                        //下载成功的回调
                        helper.setText(R.id.tv_update, "使用");
                        helper.setTextColor(R.id.tv_update, context.getResources().getColor(R.color.white));
                        helper.setBackgroundRes(R.id.tv_update, R.drawable.red_drawable_oval);
                    }

                    @Override
                    public void progress(final long progress, final long total) {
                        //下载中的回调 progress：当前进度 ，total：文件总大小
                    }

                    @Override
                    public void onError(Throwable e) {
                        //下载错误回调
                        ToastUtil.toastShortMessage(e.toString());
                    }
                });
            }else {
                 if (mOnClickSubItemListener != null) {
                  mOnClickSubItemListener.onClickUseBtn(btnUse, position,destFileDir + destFileName);
               }
            }
        });
    }
}
